#include "StdAfx.h"
#include "JWXXScene.h"

static const string sceneStrDB("<main>   <text>欢迎来到通灵兔的世界！</text>   <selector>     <text>我想算算总体的性格命运。</text>     <jumpto>destiny</jumpto>   </selector>   <selector>     <text>我想看看我的天性如何。</text>     <jumpto>inborn</jumpto>   </selector>   <selector>     <text>我想算算我性格上的特点。</text>     <jumpto>chara</jumpto>   </selector>   <selector>     <text>我想算算童年对我的影响。</text>     <jumpto>limited</jumpto>   </selector> </main>\
                               <destiny>   <text></text>   <search>destiny_destiny</search>   <search>destiny_destinymark</search>   <selector>     <text>我想算人际方面的情况。</text>     <jumpto>relationship</jumpto>   </selector>   <selector>     <text>我想算算感情。</text>     <jumpto>emotion</jumpto>   </selector>   <selector>     <text>我想看看性格里有哪些不足</text>     <jumpto>birthday</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </destiny>\
                               <relationship>   <text></text>   <search>relationship_destiny</search>   <selector>     <text>我想算我自己总体的性格。</text>     <jumpto>destiny</jumpto>   </selector>   <selector>     <text>我想算算感情。</text>     <jumpto>emotion</jumpto>   </selector>   <selector>     <text>我想算我的性格里有哪些不足。</text>     <jumpto>birthday</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </relationship>\
                               <emotion>   <text></text>   <search>emotion_destiny</search>   <selector>     <text>我想算我自己总体的性格。</text>     <jumpto>destiny</jumpto>   </selector>   <selector>     <text>我想算算人际关系。</text>     <jumpto>relationship</jumpto>   </selector>   <selector>     <text>我想看看性格里有哪些不足。</text>     <jumpto>birthday</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </emotion>\
                               <birthday> <text></text> <judge>choose_destiny</judge><search>destiny1_birthday</search> <search>destiny2_birthday</search> <search>destiny3_birthday</search> <search>destiny4_birthday</search> <search>destiny5_birthday</search> <search>destiny6_birthday</search> <search>destiny7_birthday</search> <search>destiny8_birthday</search> <search>destiny9_birthday</search><selector>     <text>我想算我自己总体的性格。</text>     <jumpto>destiny</jumpto>   </selector>   <selector>     <text>我想算算人际关系。</text>     <jumpto>relationship</jumpto>   </selector>   <selector>     <text>我想算算我的感情。</text>     <jumpto>emotion</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </birthday>\
                               <inborn>   <text>你想算青年、中年和老年哪个阶段的天性？</text><selector>     <text>我想算年轻时候的天性。</text>     <jumpto>inborn1</jumpto>   </selector>   <selector>     <text>我想算中年时候的天性。</text>     <jumpto>inborn2</jumpto>   </selector><selector>     <text>我想算晚年时候的天性。</text>     <jumpto>inborn3</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </inborn>\
                               <inborn1>   <text></text>   <search>inborn1_month</search>   <selector>     <text>我想算中年时候的天性。</text>     <jumpto>inborn2</jumpto>   </selector>   <selector>     <text>我想算晚年时候的天性。</text>     <jumpto>inborn3</jumpto>   </selector>   <selector>     <text>我想算我的后天潜能。</text>     <jumpto>talent</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </inborn1>\
                               <inborn2>   <text></text>   <search>inborn2_day</search>   <selector>     <text>我想算年轻时候的天性。</text>     <jumpto>inborn1</jumpto>   </selector>   <selector>     <text>我想算晚年时候的天性。</text>     <jumpto>inborn3</jumpto>   </selector>   <selector>     <text>我想算我的后天潜能。</text>     <jumpto>talent</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </inborn2>\
                               <inborn3>   <text></text>   <search>inborn3_year</search>   <selector>     <text>我想算年轻时候的天性。</text>     <jumpto>inborn1</jumpto>   </selector>   <selector>     <text>我想算中年时候的天性。</text>     <jumpto>inborn2</jumpto>   </selector>   <selector>     <text>我想算我的后天潜能。</text>     <jumpto>talent</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </inborn3>\
                               <talent>   <text></text>   <search>talent_talent</search>   <selector>     <text>我想算年轻时候的天性。</text>     <jumpto>inborn1</jumpto>   </selector>   <selector>     <text>我想算中年时候的天性。</text>     <jumpto>inborn2</jumpto>   </selector>   <selector>     <text>我想算晚年时候的天性。</text>     <jumpto>inborn3</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </talent>\
                               <limited>   <text></text>   <search>limitedy_limited</search>   <search>limitedg_limited</search>   <selector>     <text>我想要解除这些影响的方法。</text>     <jumpto>dismiss</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </limited>\
                               <dismiss>   <text></text>   <search>limitedr_limited</search>   <selector>     <text>我想算算童年对我的影响。</text>     <jumpto>limited</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </dismiss>\
                               <chara>   <text>你想看看你那方面能力最强、性格有何特点或不足，还是看看今年运气怎么样？</text><selector>     <text>我想看看我哪方面能力最强。</text>     <jumpto>energy</jumpto>   </selector>   <selector>     <text>我想看看我性格有何特点。</text>     <jumpto>line</jumpto>   </selector>   <selector>     <text>我想算我性格的弱点。</text>     <jumpto>missing</jumpto>   </selector>   <selector>     <text>我想算算今年的运气。</text>     <jumpto>cyear</jumpto>   </selector> </chara>\
                               <energy>   <text></text>   <judge>energy_searh</judge>		   <search>energy5_energy5</search><search>energy2_energy2</search> <search>energy1_energy1</search>  <selector>     <text>我想看看我性格有何特点。</text>     <jumpto>line/jumpto>   </selector>   <selector>     <text>我想算我性格的弱点。</text>     <jumpto>missing</jumpto>   </selector>   <selector>     <text>我想算算今年的运气。</text>     <jumpto>cyear</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </energy>\
                               <line>   <text></text>   <search>line_line</search><selector>     <text>我想看看我哪方面能力最强。</text>     <jumpto>energy</jumpto>   </selector>   <selector>     <text>我想算我性格的弱点。</text>     <jumpto>missing</jumpto>   </selector>   <selector>     <text>我想算算今年的运气。</text>     <jumpto>cyear</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </line>\
                               <missing>   <text></text>   <search>missing_missing</search><selector><text>我想看看我哪方面能力最强。</text>     <jumpto>energy</jumpto>   </selector>   <selector>     <text>我想看看我性格有何特点。</text>     <jumpto>line</jumpto>   </selector>   <selector>     <text>我想算算今年的运气。</text>     <jumpto>cyear</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </missing>\
                               <cyear>   <text></text>   <search>cyear_cyear</search> <selector>     <text>我想看看我哪方面能力最强。</text>     <jumpto>energy</jumpto>   </selector>   <selector>     <text>我想看看我性格有何特点。</text>     <jumpto>line</jumpto>   </selector>   <selector>     <text>我想算我性格的弱点。</text>     <jumpto>missing</jumpto>   </selector>   <selector>     <text>我不想算了。</text>     <jumpto>main</jumpto>   </selector> </cyear>\
                               ");
                               
                               JWXXScene::JWXXScene(void)
{
}
                               
                               JWXXScene::~JWXXScene(void)
{
}
                               JWXXScene::JWXXScene(string sceneName)
{
	JWXXScene::name = sceneName;
}
                               string JWXXScene::getXmlValue(string str,string node)
{
	string item ="";
	//TO-DO
	string begmark = "<" + node + ">";
	string endmark = "</" + node + ">";
	int beg = str.find(begmark);
	int end = str.find(endmark);
	int beglen = begmark.length();
	if ((beg >=0) && (end >=0) && (end-beg-beglen>0))
	{
		string item = str.substr(beg+beglen,end-beg-beglen);
		return item;
	}
	if (end-beg-beglen == 0)
	{
		return "";
	}
	return item;
}
                               
                               vector<string> JWXXScene::getXmlValues(string str,string node)
{
	vector<string> v_str;
	string tempStr=str;
	string endmark = "</" + node + ">";
	int endlen = endmark.length();
	while(JWXXScene::getXmlValue(tempStr,node)!="")
	{
		v_str.push_back(JWXXScene::getXmlValue(tempStr,node));
		int end = tempStr.find(endmark);
		tempStr = tempStr.substr(end+endlen,tempStr.length()-end-endlen);
	}
    
	/*
     string begmark = "<" + node + ">";
     string endmark = "</" + node + ">";
     int beg = str.find(begmark);
     int end = str.find(endmark);
     int beglen = str.length();
     if ((beg >=0) && (end >=0) && (end-beg-beglen>0))
     {
     string item = str.substr(beg+beglen,end-beg-beglen);
     if(item !="")
     v_str.push_back(item);
     }
     */
	return v_str;
}
                               
                               void JWXXScene::initTextXmlValue(string xmlStr)
{
	JWXXScene::text = JWXXScene::getXmlValue(xmlStr,"text");
}
                               void JWXXScene::initJudgeValue(string xmlStr)
{
	JWXXScene::judgeMthod = JWXXScene::getXmlValue(xmlStr,"judge");
}
                               bool JWXXScene::CallJudgeFuc(string fuc)
{
	if(fuc== "choose_destiny")
	{
		//这里执行的操作为选择命运数对应的search执行
		if(JWXXScene::search.size() !=9)//这里保证命运数能合search的表格对应
		{
			return false;
		}
		string searchStr = search.at(myManCode->mys.data - 1);
		JWXXScene::search.clear();
		JWXXScene::search.push_back(searchStr);
	}
	if(fuc == "energy_searh")
	{
		
	}
	return true;
}
                               void JWXXScene::initSearchValue(string xmlStr)
{
	JWXXScene::search = JWXXScene::getXmlValues(xmlStr,"search");
	JWXXScene::initJudgeValue(xmlStr);
	if(JWXXScene::judgeMthod !="" || JWXXScene::judgeMthod.size() > 0)
	{
		CallJudgeFuc(JWXXScene::judgeMthod);
	}
}
                               ////暂时不用
/*
 void JWXXScene::initSceneAskXmlValue(string xmlStr)
 {
 string sceneask=JWXXScene::getXmlValue(xmlStr,"ask");
 //JWXXScene::ask.search = JWXXScene::getXmlValue(sceneask,"search");
 JWXXScene::ask.search = JWXXScene::getXmlValues(sceneask,"search");////注意：scene xml文件中searh个数 如果有多个采用此模式
 JWXXScene::ask.text = JWXXScene::getXmlValue(sceneask,"text");
 }
 */
                               void JWXXScene::initSceneSelXmlValue(string xmlStr)
{
	vector<string> xmlNodeStr;
	SceneSelector s_sel;
	xmlNodeStr = JWXXScene::getXmlValues(xmlStr,"selector");
	for(int i=0;i<xmlNodeStr.size();i++)
	{
		s_sel.jumpto = JWXXScene::getXmlValue(xmlNodeStr.at(i),"jumpto");
		s_sel.text = JWXXScene::getXmlValue(xmlNodeStr.at(i),"text");
		JWXXScene::selector.push_back(s_sel);
	}
}
                               
                               void JWXXScene::SceneInitFromXMLStr(std::string str)//暂时不用
{
    extern string JWXXSceneText;
    //Modfy by Dzik 20120821
#if defined(WIN32)
	JWXXScene::senceStr = JWXXScene::getXmlValue(sceneStrDB,str);
#else
    JWXXScene::senceStr = JWXXScene::getXmlValue(JWXXSceneText,str);
#endif
	JWXXScene::initTextXmlValue(senceStr);
	//JWXXScene::initSceneAskXmlValue(senceStr);//暂时不用
	JWXXScene::initSceneSelXmlValue(senceStr);
}
                               bool JWXXScene::SceneInit(string birthDayStr,string curDateStr)
{
    extern string JWXXSceneText;
	JWXXScene::birthDate = birthDayStr;
	JWXXScene::curDate = curDateStr;
	JWXXScene::myManCode = new JWXXManCodeBase(birthDayStr,curDateStr);
	myManCode->InitData();
	JWXXSerchData  *searchObj = new JWXXSerchData(myManCode);
	bool init = true;
    //Modify by Dzik 20120821
#if defined(WIN32)
	JWXXScene::senceStr = JWXXScene::getXmlValue(sceneStrDB,JWXXScene::name);
#else
    JWXXScene::senceStr = JWXXScene::getXmlValue(JWXXSceneText,JWXXScene::name);
#endif
	if((senceStr =="") || (senceStr.size() == 0))
		init = false;//在场景字符串中找不到指定场景
	JWXXScene::initTextXmlValue(senceStr);
	JWXXScene::initSearchValue(senceStr);
	//JWXXScene::initSceneAskXmlValue(senceStr);//暂时不用
	JWXXScene::initSceneSelXmlValue(senceStr);
	if(init && JWXXScene::search.size() >0)
	{
		JWXXScene::text = searchObj->getSearchResult(JWXXScene::search);
	}
	return init;
}